// app.js
App({
    onLaunch() {
        // 展示本地存储能力
        const logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        // console.log('nhb')
        // console.log(this)
        var that=this;
        // wx.getShareInfo({
        //     shareTicket: true,
        //     success: function(res) {
        //         console.log(res)
        //     },
        //     fail: function(res) {
        //         console.log(res)
        //     }
        // })


    },
    onShow(options) {
        var that=this
        if (!wx.getStorageSync("token")) {
            wx.showModal({
                title: '温馨提示',
                content: '亲，授权微信登录后才能正常使用小程序功能',
                success(res) {
                    //如果用户点击了确定按钮
                    if (res.confirm) {
                        wx.getUserProfile({
                            desc: '获取你的昵称、头像、地区及性别',
                            success: profile => {
                                that.globalData.userInfo = profile
                                wx.login({
                                    success: res => {
                                        that.getTokenAndUserId(profile, res.code)  //res.code登录凭证
                                    }
                                })
                            },
                            fail: res => {
                                //拒绝授权
                                wx.showToast({
                                    title: '您拒绝了请求,不能正常使用小程序',
                                    icon: 'error',
                                    duration: 2000
                                });
                                return;
                            }
                        });
                    } else if (res.cancel) {
                        //如果用户点击了取消按钮
                        wx.showToast({
                            title: '您拒绝了请求,不能正常使用小程序',
                            icon: 'error',
                            duration: 2000
                        });
                        return;
                    }
                }
            });
        }
    },
    // 发送请求获取token
    getTokenAndUserId(profile, code) {
        var that=this
        wx.request({
            url: that.globalData.url + "/ft-user/login",
            method: 'POST',
            header: {'content-type': 'application/json'},
            data: {
                encryptedData: profile.encryptedData,
                iv: profile.iv,
                js_code: code,
                provider: 'weixin',
                userInfo: profile.rawData
            },
            success(res) {
                let data = res.data.data
                console.log(data)
                wx.showToast({
                    title: 'familyId:' + that.globalData.familyId,
                })
                if (data.token != null) {
                    wx.setStorageSync("token", data.token);
                    // wx.setStorageSync("userInfo", data.userInfo);
                }
                if (that.globalData.familyId) that.globalData.home.bindUserToFamily(that.globalData.familyId)
                that.globalData.home.getFamilyData(data.token)
            }
        })
    },
    //全局数据
    globalData: {
        url: 'https://pg-family-tree-ranpovkkyd.cn-hangzhou.fcapp.run',
        userInfo: null,
        familyId: null,
        token: null,
        home: null //首页
    }
})
